package com.egoo.ticket.server.utils.common;

import com.egoo.ticket.server.pojo.dto.CreateActivitiXmlDto;
import com.egoo.ticket.server.pojo.vo.WorkOrderTypeNodeVo;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author Administrator
 */
@Slf4j
public class XmlUtils {

	/**
	 * TODO 生成activiti的xml方法
	 */
	public static void createActivitiXml(CreateActivitiXmlDto createActivitiXmlDto) {
		try {
			// 1、创建document对象
			Document document = DocumentHelper.createDocument();
			// 2、创建根节点Definitions
			Element definitions = document.addElement("definitions", "http://www.omg.org/spec/BPMN/20100524/MODEL");
			// 3、向Definitions节点添加属性
			definitions.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
			definitions.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
			definitions.addAttribute("xmlns:activiti", "http://activiti.org/bpmn");
			definitions.addAttribute("xmlns:bpmndi", "http://www.omg.org/spec/BPMN/20100524/DI");
			definitions.addAttribute("xmlns:omgdc", "http://www.omg.org/spec/DD/20100524/DC");
			definitions.addAttribute("xmlns:omgdi", "http://www.omg.org/spec/DD/20100524/DI");
			definitions.addAttribute("typeLanguage", "http://www.w3.org/2001/XMLSchema");
			definitions.addAttribute("expressionLanguage", "http://www.w3.org/1999/XPath");
			definitions.addAttribute("targetNamespace", "http://www.activiti.org/test");
			// 4、生成子节点及子节点内容
			Element process = definitions.addElement("process");
			process.addAttribute("id",
					createActivitiXmlDto.getWorkOrderTypeName() + createActivitiXmlDto.getWorkOrderTypeId());
			process.addAttribute("name", createActivitiXmlDto.getWorkOrderTypeName());
			process.addAttribute("isExecutable", "true");
			Element startEvent = process.addElement("startEvent");
			startEvent.addAttribute("id", "start");
			startEvent.addAttribute("name", "start");
			startEvent.addAttribute("activiti:initiator", "applicant");
			startEvent.setText("");
			Element userTask = process.addElement("userTask");
			userTask.addAttribute("id", "_" + createActivitiXmlDto.getWorkOrderTypeNodeVoStartName()
					+ createActivitiXmlDto.getWorkOrderTypeNodeVoStartId());
			userTask.addAttribute("name", createActivitiXmlDto.getWorkOrderTypeNodeVoStartName());
			userTask.addAttribute("activiti:assignee", "${applicant}");
			userTask.setText("");
			Element sequenceFlow = process.addElement("sequenceFlow");
			sequenceFlow.addAttribute("id", "flow1");
			sequenceFlow.addAttribute("sourceRef", "start");
			sequenceFlow.addAttribute("targetRef", "_" + createActivitiXmlDto.getWorkOrderTypeNodeVoStartName()
					+ createActivitiXmlDto.getWorkOrderTypeNodeVoStartId());
			sequenceFlow.setText("");
			// 循环创建流转节点
			List<WorkOrderTypeNodeVo> flowNodeList =
					createActivitiXmlDto.getFlowNodeList();
			for (int i = 0; i < flowNodeList.size(); i++) {
				Element userTask1 = process.addElement("userTask");
				userTask1.addAttribute("id", "_" + flowNodeList.get(i).getName() + flowNodeList.get(i).getId());
				userTask1.addAttribute("name", flowNodeList.get(i).getName());
				userTask1.addAttribute("activiti:assignee", "张三");
				userTask1.setText("");
			}
//			Element extensionElements = userTask1.addElement("extensionElements");
////			Element activitiTaskListener = extensionElements.addNamespace("activiti","taskListener");
//			Element activitiTaskListener = extensionElements.addElement("activiti","taskListener");
//			activitiTaskListener.addAttribute("event","complete");
//			activitiTaskListener.addAttribute("class","com.egoo.ews.service.multiActiviti.SignTaskListener");
//			Element multiInstanceLoopCharacteristics = userTask1.addElement("multiInstanceLoopCharacteristics");
//			multiInstanceLoopCharacteristics.addAttribute("isSequential","false");
//			multiInstanceLoopCharacteristics.addAttribute("activiti:collection","${leaderList}");
//			multiInstanceLoopCharacteristics.addAttribute("activiti:elementVariable","leader");
//			Element completionCondition = multiInstanceLoopCharacteristics.addElement("completionCondition");
//			completionCondition.setText("${nrOfCompletedInstances == 1}");

			// 5、设置生成xml的格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 设置编码格式
			format.setEncoding("utf-8");

			// 6、生成xml文件
			File file = new File(createActivitiXmlDto.getXmlUrl());
			XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
			// 设置是否转义，默认使用转义字符
			writer.setEscapeText(false);
			writer.write(document);
			writer.close();
			log.info("createActivitiXml succeed");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
